<?php

namespace Home\Model\Ajax;

class TagList extends AbstractAjax
{
    // TAG分类列表
    public function handle()
    {
        $post_tags     = I('request.tags', '', 'trim');
        $post_tags_arr = array();
        if (!empty($post_tags)) {
            $post_tags_arr = explode('__yangtata__', $post_tags);
        }
        $sort = 'sort DESC, count_question DESC';
        $res  = D('Tag')->order($sort)->limit(30)->select();
        foreach ($res as $var) {
            $tags_list[] = $var['tag'];
        }
        foreach ($post_tags_arr as $var) {
            if (!in_array($var, $tags_list)) {
                $tags_list[] = $var;
            }
        }
        $list = array_filter($tags_list);
        foreach ($list as $var) {
            $li['tag']    = $var;
            $li['select'] = '';
            if (!empty($post_tags_arr) && in_array($var, $post_tags_arr)) {
                $li['select'] = 'select';
            }
            $taglist[] = $li;
        }
        $list = $taglist;

        $this->assign('list', $list);
        $html            = $this->fetch('ajax_tag_list');
        $ajax['status']  = 1;
        $ajax['content'] = $html;
        return $ajax;
    }
}